﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Oracle.DataAccess.Client;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private OracleDataReader reader;
        OracleConnection connection;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string connectionString = "User Id=cndr;Password=qwerty;Data Source=(DESCRIPTION=" +
            "(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))" +
            "(CONNECT_DATA=(SID=xe)));";
            connection = new OracleConnection();
            
                connection.ConnectionString = connectionString;
                connection.Open();
                this.Text="Computer store - "+connection.State.ToString();

                //временно
                OracleCommand command = connection.CreateCommand();
                string sql = "SELECT * FROM Motherboard";
                command.CommandText = sql;

                reader = command.ExecuteReader();
                //this.dataGridView1.DataSource = reader;
                
                DataSet ds = new DataSet();
                DataTable dt = new DataTable("Table");
                ds.Tables.Add(dt);
                ds.Load(reader, LoadOption.PreserveChanges, ds.Tables[0]);
                dataGridView1.DataSource = ds.Tables[0];
                /*while (reader.Read())
                {
                    //string myField = (string)reader[1];//"MYFIELD"
                    string myField = reader[1].ToString();
                    //this.textBox1.Text = this.textBox1.Text +"   "+ myField;
                }*/
                //connection.Close();
                //this.textBox1.Text = this.textBox1.Text + "Closed";
            
        }

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            OracleCommand command = connection.CreateCommand();
            string sql = "SELECT * FROM Motherboard";
            command.CommandText = sql;

            reader = command.ExecuteReader();
            this.Text = reader[e.RowIndex].ToString();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            connection.Close();
        }
    }
}
